home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_354 / mxmlib / libmain.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  382 lines

  1. /* $Revision Header *** Header built automatically - do not edit! ***********
  2.  *
  3.  *    (C) Copyright 1990 by MXM
  4.  *
  5.  *    Name .....: LibMain.c
  6.  *    Created ..: Saturday 06-Jan-90 23:30
  7.  *    Revision .: 8
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    01-Apr-90       Olsen           Rework for Aztec 5.0
  12.  *    25-Jan-90       Olsen           Did some cleanups
  13.  *    14-Jan-90       Olsen           Renamed to mxm.library
  14.  *    07-Jan-90       Olsen           - Empty log message -
  15.  *    07-Jan-90       Olsen           Appended new functions
  16.  *    06-Jan-90       Olsen           - Empty log message -
  17.  *    06-Jan-90       Olsen           - Empty log message -
  18.  *    06-Jan-90       Olsen           Created this file!
  19.  *
  20.  * $Revision Header ********************************************************/
  21.  
  22.     /* Imported from MXM.c */
  23.  
  24. extern LONG Revision;
  25.  
  26.     /* ASCII library ID. */
  27.  
  28. char *LibName    = "mxm.library";
  29. char *LibId    = "mxm.library 34.14 (01 Apr 1990)\r\n";
  30.  
  31.     /* External data, defined in asm startup code. */
  32.  
  33. extern struct Resident     LibRomTag;
  34. extern LONG         LibInit();
  35.  
  36.     /* Protos for this module. */
  37.  
  38. struct MXMBase *    LibMain(struct MXMBase *,BPTR);
  39. struct MXMBase *    LibOpen(VOID);
  40. BPTR            LibClose(VOID);
  41. BPTR            LibExpunge(VOID);
  42.  
  43.     /* And now for the pragmas... */
  44.  
  45. #pragma amicall(MXMBase,0x06,LibOpen())
  46. #pragma amicall(MXMBase,0x0C,LibClose())
  47. #pragma amicall(MXMBase,0x12,LibExpunge())
  48.  
  49.     /* The library initialization. */
  50.  
  51. #pragma regcall(LibMain(d0,a0))
  52.  
  53.     /* The structure expected by the library loader (auto-init). */
  54.  
  55. struct InitTable
  56. {
  57.     ULONG     it_DataSize;    /* Data size to allocate. */
  58.     APTR    *it_FuncTable;    /* Pointer to function table. */
  59.     APTR     it_DataInit;    /* Pointer to data initializers (remember InitStruct()?). */
  60.     APTR     it_InitFunc;    /* The real library init function. */
  61. };
  62.  
  63.     /* The list of library functions. */
  64.  
  65. VOID *LibFuncTab[] =
  66. {
  67.     LibOpen,        /* Standard library routines. */
  68.     LibClose,
  69.     LibExpunge,
  70.     NULL,            /* Reserved, leave it NULL. */
  71.  
  72.     AllocRem,
  73.     FreeRem,
  74.     CreateFuncTask,
  75.     DeleteFuncTask,
  76.     CreateFuncProc,
  77.     MovePointer,
  78.     WriteConsole,
  79.     DeletePrinterSupport,
  80.     CreatePrinterSupport,
  81.     StartPrinterDump,
  82.     StopPrinterDump,
  83.     DeleteTimer,
  84.     CreateTimer,
  85.     WaitForTimer,
  86.     TimeDelay,
  87.     KeyConvert,
  88.     InvertKey,
  89.     EraseGadget,
  90.     RefreshGadget,
  91.     GetLongInt,
  92.     SetLongInt,
  93.     GetGadgetString,
  94.     SetGadgetString,
  95.     GetToggleGadget,
  96.     SetToggleGadget,
  97.     SetWaitPointer,
  98.     PopRequest,
  99.     PopMenu,
  100.     Announce,
  101.     CheckSumCRC,
  102.     CheckSumSimple,
  103.     CryptBlock,
  104.     IsASCII,
  105.     IsPrintable,
  106.     ToUpper,
  107.     UStrCmp,
  108.     Random,
  109.     FindFileWindow,
  110.     DeleteHiddenRPort,
  111.     CreateHiddenRPort,
  112.     EnableWindow,
  113.     DisableWindow,
  114.  
  115.     (VOID *)-1        /* End marker. */
  116. };
  117.  
  118.     /* The romtag needs this. */
  119.  
  120. struct InitTable LibInitTab =
  121. {
  122.     (ULONG)sizeof(struct MXMBase),    /* Lib base. */
  123.     (APTR)LibFuncTab,        /* Function table. */
  124.     NULL,                /* No data init table (we'll do autoinit). */
  125.     (APTR)LibInit            /* Lib init routine. */
  126. };
  127.  
  128.     /* Library auxilary data. */
  129.  
  130. struct IOStdReq        *kc_ConStdReq;
  131. struct KeyEquivalent    *EquList;
  132. struct KeyMap        *ik_KeyMap;
  133. struct SignalSemaphore    *PopSemaphore;
  134.  
  135.     /* The libraries this library needs. */
  136.  
  137. APTR             IntuitionBase;
  138. APTR             GfxBase;
  139. APTR             LayersBase;
  140. APTR             DOSBase;
  141. APTR             ConsoleDevice;
  142.  
  143.     /* Global segment list (we only need one). */
  144.  
  145. BPTR             LibSegList = ZERO;
  146.  
  147.     /* LibMain(MXMBase,SegList):
  148.      *
  149.      *    Does the main library initialization, expects
  150.      *    arguments in registers.
  151.      */
  152.  
  153. struct MXMBase *
  154. LibMain(MXMBase,SegList)
  155. struct MXMBase *MXMBase;
  156. BPTR SegList;
  157. {
  158.         /* We don't wish to be initialized twice,
  159.          * the check for LibSegList will hopefully
  160.          * insure this.
  161.          */
  162.  
  163.     if(!LibSegList)
  164.     {
  165.             /* Remember segment list. */
  166.  
  167.         LibSegList = SegList;
  168.  
  169.             /* Try to open the required libraries. */
  170.  
  171.         if(!(IntuitionBase = (APTR)OpenLibrary("intuition.library",LIBRARY_MINIMUM)))
  172.             goto Failed;
  173.  
  174.         if(!(GfxBase = (APTR)OpenLibrary("graphics.library",LIBRARY_MINIMUM)))
  175.             goto Failed;
  176.  
  177.         if(!(LayersBase = (APTR)OpenLibrary("layers.library",LIBRARY_MINIMUM)))
  178.             goto Failed;
  179.  
  180.         if(!(DOSBase = (APTR)OpenLibrary("dos.library",LIBRARY_MINIMUM)))
  181.             goto Failed;
  182.  
  183.         if(!(kc_ConStdReq = (struct IOStdReq *)AllocMem(sizeof(struct IOStdReq),MEMF_PUBLIC | MEMF_CLEAR)))
  184.             goto Failed;
  185.  
  186.         if(OpenDevice("console.device",-1,kc_ConStdReq,0))
  187.             goto Failed;
  188.  
  189.         if(!(EquList = (struct KeyEquivalent *)AllocMem(sizeof(struct KeyEquivalent) * 94,MEMF_PUBLIC | MEMF_CLEAR)))
  190.             goto Failed;
  191.  
  192.         if(!(ik_KeyMap = (struct KeyMap *)AllocMem(sizeof(struct KeyMap),MEMF_PUBLIC | MEMF_CLEAR)))
  193.             goto Failed;
  194.  
  195.         if(!(PopSemaphore = (struct SignalSemaphore *)AllocMem(sizeof(struct SignalSemaphore),MEMF_PUBLIC | MEMF_CLEAR)))
  196.             goto Failed;
  197.  
  198.         InitSemaphore(PopSemaphore);
  199.  
  200.         ConsoleDevice = (APTR)kc_ConStdReq -> io_Device;
  201.  
  202.             /* Fill in the library head. */
  203.  
  204.         MXMBase -> LibNode . lib_Node . ln_Type    = NT_LIBRARY;
  205.         MXMBase -> LibNode . lib_Node . ln_Name    = LibName;
  206.         MXMBase -> LibNode . lib_Flags        = LIBF_SUMUSED | LIBF_CHANGED;
  207.         MXMBase -> LibNode . lib_Version    = LibRomTag . rt_Version;
  208.         MXMBase -> LibNode . lib_Revision    = Revision;
  209.         MXMBase -> LibNode . lib_IdString    = (APTR)LibId;
  210.  
  211.         MXMBase -> IntuitionBase    = IntuitionBase;
  212.         MXMBase -> GfxBase        = GfxBase;
  213.         MXMBase -> LayersBase        = LayersBase;
  214.         MXMBase -> DOSBase        = DOSBase;
  215.         MXMBase -> ConsoleDevice    = ConsoleDevice;
  216.  
  217.         return(MXMBase);
  218.  
  219. Failed:        if(IntuitionBase)
  220.             CloseLibrary(IntuitionBase);
  221.  
  222.         if(GfxBase)
  223.             CloseLibrary(GfxBase);
  224.  
  225.         if(LayersBase)
  226.             CloseLibrary(LayersBase);
  227.  
  228.         if(ConsoleDevice)
  229.             CloseDevice(kc_ConStdReq);
  230.  
  231.         if(DOSBase)
  232.             CloseLibrary(DOSBase);
  233.  
  234.         if(EquList)
  235.             FreeMem(EquList,sizeof(struct KeyEquivalent) * 94);
  236.  
  237.         if(kc_ConStdReq)
  238.             FreeMem(kc_ConStdReq,sizeof(struct IOStdReq));
  239.  
  240.         if(ik_KeyMap)
  241.             FreeMem(ik_KeyMap,sizeof(struct KeyMap));
  242.  
  243.         if(PopSemaphore)
  244.             FreeMem(PopSemaphore,sizeof(struct SignalSemaphore));
  245.  
  246.         LibSegList = ZERO;
  247.  
  248.         return(NULL);
  249.     }
  250. }
  251.  
  252.     /* LibOpen():
  253.      *
  254.      *    Library open routine.
  255.      */
  256.  
  257. struct MXMBase *
  258. LibOpen()
  259. {
  260.     struct MXMBase *MXMBase;
  261.  
  262.         /* Check if everything is in good shape. */
  263.  
  264.     if(!IntuitionBase || !GfxBase || !LayersBase || !DOSBase || !ConsoleDevice || !EquList || !kc_ConStdReq || !ik_KeyMap)
  265.         return(NULL);
  266.  
  267.         /* Increment open count. */
  268.  
  269.     MXMBase -> LibNode . lib_OpenCnt++;
  270.     MXMBase -> LibNode . lib_Flags &= ~LIBF_DELEXP;
  271.  
  272.         /* Return base pointer. */
  273.  
  274.     return(MXMBase);
  275. }
  276.  
  277.     /* LibClose():
  278.      *
  279.      *    Closes the library.
  280.      */
  281.  
  282. BPTR
  283. LibClose()
  284. {
  285.     struct MXMBase    *MXMBase;
  286.     BPTR         SegList = ZERO;
  287.  
  288.         /* Time for an expunge? */
  289.  
  290.     if(MXMBase -> LibNode . lib_OpenCnt > 0)
  291.     {
  292.         MXMBase -> LibNode . lib_OpenCnt--;
  293.  
  294.         SegList = LibExpunge();
  295.     }
  296.     else
  297.         Alert(AT_Recovery | AG_CloseLib,"MXM");
  298.  
  299.     return(SegList);
  300. }
  301.  
  302.     /* LibExpunge(MXMBase):
  303.      *
  304.      *    Expunge library, careful: this can be called by
  305.      *    ramlib while the rest of the library doesn't know
  306.      *    about it.
  307.      */
  308.  
  309. BPTR
  310. LibExpunge()
  311. {
  312.     struct MXMBase    *MXMBase;
  313.     BPTR         SegList = ZERO;
  314.  
  315.         /* Is user count zero, the delayed expunge flag
  316.          * set and the library seglist valid?
  317.          */
  318.  
  319.     if(MXMBase -> LibNode . lib_OpenCnt == 0 && (MXMBase -> LibNode . lib_Flags & LIBF_DELEXP) && LibSegList)
  320.     {
  321.             /* Remember segment list. */
  322.  
  323.         SegList = LibSegList;
  324.  
  325.         LibSegList = ZERO;
  326.  
  327.             /* Remove library from lib list. */
  328.  
  329.         Remove((struct Node *)MXMBase);
  330.  
  331.             /* Some paranoid action. */
  332.  
  333.         MXMBase -> LibNode . lib_Flags &= ~LIBF_DELEXP;
  334.  
  335.             /* Close what we've opened. */
  336.  
  337.         if(IntuitionBase)
  338.             CloseLibrary(IntuitionBase);
  339.  
  340.         if(GfxBase)
  341.             CloseLibrary(GfxBase);
  342.  
  343.         if(LayersBase)
  344.             CloseLibrary(LayersBase);
  345.  
  346.         if(ConsoleDevice)
  347.             CloseDevice(kc_ConStdReq);
  348.  
  349.         if(DOSBase)
  350.             CloseLibrary(DOSBase);
  351.  
  352.         if(EquList)
  353.             FreeMem(EquList,sizeof(struct KeyEquivalent) * 94);
  354.  
  355.         if(kc_ConStdReq)
  356.             FreeMem(kc_ConStdReq,sizeof(struct IOStdReq));
  357.  
  358.         if(ik_KeyMap)
  359.             FreeMem(ik_KeyMap,sizeof(struct KeyMap));
  360.  
  361.         if(PopSemaphore)
  362.             FreeMem(PopSemaphore,sizeof(struct SignalSemaphore));
  363.  
  364.             /* Free library mem. */
  365.  
  366.         FreeMem((char *)MXMBase - MXMBase -> LibNode . lib_NegSize,MXMBase -> LibNode . lib_NegSize + MXMBase -> LibNode .lib_PosSize);
  367.     }
  368.     else
  369.     {
  370.             /* In any other case we'll set the delayed
  371.              * expunge flag (so next expunge call will
  372.              * hopefully wipe us from the lib list.
  373.              */
  374.  
  375.         MXMBase -> LibNode . lib_Flags |= LIBF_DELEXP;
  376.     }
  377.  
  378.         /* Return segment list pointer. */
  379.  
  380.     return(SegList);
  381. }
  382.